package com.lcb.rpc;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

public class RPCClient {

    private Connection connection;
    private Channel channel;
    private String requestQueueName = "rpc_queue";
    private String replyQueueName;
    private Consumer consumer;

    private static final String IP_ADDRESS = "10.0.127.108";
    private static final int PORT = 5672;

    public RPCClient() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setHost(IP_ADDRESS);
        factory.setPort(PORT);
        //创建连接
        connection = factory.newConnection();
        //创建信道
        channel = connection.createChannel();
        replyQueueName = channel.queueDeclare().getQueue();
        consumer = new DefaultConsumer(channel);
        channel.basicConsume(replyQueueName, true, consumer);
    }

    public String call(String message) throws IOException {
        String responce = null;
        String corrId = UUID.randomUUID().toString();
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().correlationId(corrId).replyTo(replyQueueName).build();
        channel.basicPublish("", requestQueueName, properties, message.getBytes());

        while(true){
           // consumer.
        }


    }


    public static void main(String[] args) {

    }
}
